package cn.stylefeng.guns.modular.ums.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.rms.entity.Company;
import cn.stylefeng.guns.modular.rms.entity.CompanyPosition;
import cn.stylefeng.guns.modular.rms.mapper.CompanyMapper;
import cn.stylefeng.guns.modular.rms.mapper.CompanyPositionMapper;
import cn.stylefeng.guns.modular.rms.mapper.CourseMapper;
import cn.stylefeng.guns.modular.rms.mapper.OuttaskMapper;
import cn.stylefeng.guns.modular.rms.model.params.CompanyPositionParam;
import cn.stylefeng.guns.modular.rms.model.params.CourseParam;
import cn.stylefeng.guns.modular.rms.model.params.OuttaskParam;
import cn.stylefeng.guns.modular.rms.model.result.CompanyPositionResult;
import cn.stylefeng.guns.modular.rms.model.result.CourseResult;
import cn.stylefeng.guns.modular.rms.model.result.OuttaskResult;
import cn.stylefeng.guns.modular.ums.entity.ShareLog;
import cn.stylefeng.guns.modular.ums.mapper.MemberMapper;
import cn.stylefeng.guns.modular.ums.mapper.ShareLogMapper;
import cn.stylefeng.guns.modular.ums.model.params.ShareLogParam;
import cn.stylefeng.guns.modular.ums.model.result.MemberResult;
import cn.stylefeng.guns.modular.ums.model.result.ShareLogResult;
import  cn.stylefeng.guns.modular.ums.service.ShareLogService;
import cn.stylefeng.guns.modular.wms.mapper.ApplyReadMapper;
import cn.stylefeng.guns.modular.wms.mapper.JobExpectMapper;
import cn.stylefeng.guns.modular.wms.mapper.ResumeMapper;
import cn.stylefeng.guns.modular.wms.mapper.WorkExpMapper;
import cn.stylefeng.guns.modular.wms.model.params.ApplyReadParam;
import cn.stylefeng.guns.modular.wms.model.params.JobExpectParam;
import cn.stylefeng.guns.modular.wms.model.params.ResumeParam;
import cn.stylefeng.guns.modular.wms.model.params.WorkExpParam;
import cn.stylefeng.guns.modular.wms.model.result.ApplyReadResult;
import cn.stylefeng.guns.modular.wms.model.result.JobExpectResult;
import cn.stylefeng.guns.modular.wms.model.result.WorkExpResult;
import cn.stylefeng.guns.utils.StringUtil;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 分享记录表 服务实现类
 * </p>
 *
 * @author yzb
 * @since 2022-04-28
 */
@Service
public class ShareLogServiceImpl extends ServiceImpl<ShareLogMapper, ShareLog> implements ShareLogService {
    @Resource
    private CompanyPositionMapper companyPositionMapper;
    @Resource
    private ResumeMapper resumeMapper;
    @Resource
    private MemberMapper memberMapper;
    @Resource
    private CompanyMapper companyMapper;
    @Resource
    private CourseMapper courseMapper;
    @Resource
    private OuttaskMapper outtaskMapper;

    @Override
    public void add(ShareLogParam param){
        ShareLog entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(ShareLogParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(ShareLogParam param){
        ShareLogResult result=this.findBySpec(param);
        if (result!=null){
            param.setId(result.getId());
            ShareLog oldEntity = getOldEntity(param);
            ShareLog newEntity = getEntity(param);
            ToolUtil.copyProperties(newEntity, oldEntity);
            this.updateById(newEntity);
        }else{
            this.add(param);
        }
    }

    @Override
    public ShareLogResult findBySpec(ShareLogParam param){
        return baseMapper.findBySpec(param);
    }

    @Override
    public List<ShareLogResult> findListBySpec(ShareLogParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(ShareLogParam param){
        Page pageContext = getPageContext();
        IPage<ShareLogResult> page = this.baseMapper.customPageList(pageContext, param);
        page.getRecords().forEach(item -> {
//            类型：0-职位分享，1-课程分享，3-外包分享
            if (item.getType()==0){
                item.setPosition(companyPositionMapper.findBySpec(new CompanyPositionParam().setId(item.getDataId())));
                //招聘者信息
                MemberResult memberResult=memberMapper.getMemberById(Integer.parseInt(item.getPosition().getCreateBy()));
                item.setMemberAvatar(memberResult.getAvatar());
                item.setMemberName(memberResult.getName());
                item.setMemberPostName(memberResult.getPostName());
                //企业信息
                Company company=companyMapper.selectById(item.getCompanyId());
                item.setCompanyName(company.getName());
                item.setCompanyStaffSize(company.getStaffSize());
                item.setCompanyNature(company.getNature());
            }else if(item.getType()==1){//课程分享
                item.setCourse(courseMapper.findBySpec(new CourseParam().setId(item.getDataId())));
            }else if(item.getType()==2){//外包分享
                item.setTask(outtaskMapper.findBySpec(new OuttaskParam().setId(item.getDataId())));
            }
        });
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    public LayuiPageInfo findPageBySpecForAdmin(ShareLogParam param) {
        Page pageContext = getPageContext();
        IPage<ShareLogResult> page = this.baseMapper.customPageListForAdmin(pageContext, param);
        page.getRecords().forEach(item -> {
            if(StringUtil.isNotEmpty(item.getDataName1())){
                item.setDataName(item.getDataName1());
            }else if(StringUtil.isNotEmpty(item.getDataName2())){
                item.setDataName(item.getDataName2());
            }else if(StringUtil.isNotEmpty(item.getDataName3())){
                item.setDataName(item.getDataName3());
            }
        });
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(ShareLogParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private ShareLog getOldEntity(ShareLogParam param) {
        return this.getById(getKey(param));
    }

    private ShareLog getEntity(ShareLogParam param) {
        ShareLog entity = new ShareLog();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
